package com.zj.core.dialog

import android.app.Activity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.LinearLayout
import com.dialogutils.ToastUtil
import com.fileutils.BaseConstant.PATTERN_BED
import com.fileutils.NumberUtils
import com.jniutils.DataCollUtil
import com.zj.core.R
import com.zj.core.base.BaseDialog
import com.zj.core.interfaces.TextMessageIml
import com.zj.core.tools.ViewPosition
import com.zj.core.tools.VoiceAsrTools
import com.zj.core.ui.BindNurseNameDialogUi
import java.lang.StringBuilder

/**
 * 绑定护士姓名DigLog
 */
class BindNurseNameDialog(
    activity: Activity?,
    title: String?,
    bindNurseName: TextMessageIml?
) : BaseDialog(activity), View.OnClickListener,TextMessageIml{
    private var mActivity: Activity? = activity
    private var mCallIml: TextMessageIml? = bindNurseName
    private var mBindNurseNameDialogView: BindNurseNameDialogUi? = null
    private var mTitle: String? = title
    private var mBuilder: StringBuilder? = null
    private var mHandle:VoiceAsrTools?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val width: Int = ViewPosition().getScreenWidth(this.context)
        val height: Int = (ViewPosition().getScreenHeight(this.context) * 0.95).toInt()
        val params = LinearLayout.LayoutParams(width, height, 0f)

        mBindNurseNameDialogView = BindNurseNameDialogUi(context)
        mBindNurseNameDialogView?.apply {
            setContentView(common_dialog_root_view, params)
            common_dialog_cancel_tv?.setOnClickListener(this@BindNurseNameDialog)
            common_dialog_confirm_tv?.setOnClickListener(this@BindNurseNameDialog)
            common_dialog_root_view?.setOnClickListener(this@BindNurseNameDialog)
            common_dialog_message_tv?.setText(mTitle)
            common_dialog_message_tv?.setSelection(common_dialog_message_tv?.text.toString().length)
            guide_line_lly?.setOnClickListener(this@BindNurseNameDialog)
        }
        mHandle= VoiceAsrTools().setVoiceImage(mBindNurseNameDialogView?.voice_img_view).setHandleListener(this)
        mBuilder = StringBuilder()
        setCanceledOnTouchOutside(true)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        mHandle?.onKeyDown(keyCode,mActivity,mBindNurseNameDialogView?.common_dialog_message_tv?.windowToken)
        return true
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
        mHandle?.onKeyUp(keyCode,mActivity)
        return false
    }

    override fun messageResult(str: String) {
        specificVoice(mBindNurseNameDialogView?.common_dialog_message_tv?.text.toString() + " " + str).apply {
            mBindNurseNameDialogView?.common_dialog_message_tv?.setText(this)
            mBindNurseNameDialogView?.common_dialog_message_tv?.setSelection(this.length)
        }
    }

    fun specificVoice(str: String): String {
        DataCollUtil.patternValue(PATTERN_BED, str)?.let {
            mBuilder?.clear()
            if (it != "" && it != "床") {
                val index = str.indexOf(it)
                mBuilder?.append(str.replace(it, ""))
                return mBuilder?.insert(index, NumberUtils.CharDateFormat(it)).toString()
            }
        }
        return str
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            // 确定
            R.id.common_dialog_confirm_tv -> {
                if(mBindNurseNameDialogView?.common_dialog_message_tv?.text.toString().isNotBlank()){
                    mCallIml?.messageResult(mBindNurseNameDialogView?.common_dialog_message_tv?.text.toString())
                    dismiss()
                }else{
                    ToastUtil.showShort(mActivity,"请输入账号对应的护士姓名")
                }
            }
            R.id.parent_cs_layout -> dismiss()
            R.id.guide_line_lly -> { }
        }
    }
}